package com.thibal.lylo;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class LyloParser {

	private static final String SALLE = "string style2";
	private static final String BANDNAME = "tk-ff-meta-web-pro titre15";
	private static final String BANDDESCRIPTION = "tk-ff-meta-web-pro titre13";
	private static final String PRICEINSTYLE = "font-size:0.8em";
	private static final String PRICEINSPAN = "titre13";


	private TagNode rootNode;

	public LyloParser(URL htmlPage) throws IOException
	{
		HtmlCleaner cleaner = new HtmlCleaner();
		rootNode = cleaner.clean(htmlPage);
	}

	List<LyloRow> getLinksByClass(String CSSClassname)
	{
		List<LyloRow> res = new ArrayList<LyloRow>();
		TagNode linkElements[] = rootNode.getElementsByName("div", true);
		for (int i = 0; linkElements != null && i < linkElements.length; i++)
		{
			String classType = linkElements[i].getAttributeByName("id");
			if (classType != null && classType.equals(CSSClassname))
			{
				TagNode trElements[] = linkElements[i].getElementsByName("tr", true);
				for(int j = 0; trElements != null && j < trElements.length; j++)
				{
					LyloRow row = new LyloRow();

					TagNode imgElements[] = trElements[j].getElementsByName("img", true);
					if(imgElements.length >= 2)
					{
						row.setQuarterName(imgElements[0].getAttributeByName("src"));
						row.setStyle(imgElements[1].getAttributeByName("src"));
					}
					TagNode aElements[] = trElements[j].getElementsByName("a", true);

					for(int k = 0; aElements != null && k < aElements.length; k++)
					{
						String classAttribute = aElements[k].getAttributeByName("class");
						if(classAttribute != null && classAttribute.equals(SALLE))
						{
							row.setPlaceName(aElements[k].getText().toString().replace("&nbsp;", "").replace("&amp;", "&"));
							row.setPlaceUrl(aElements[k].getAttributeByName("href"));
						}
					}
					
					TagNode strongElements[] = trElements[j].getElementsByName("strong", true);
					for(int k = 0; strongElements != null && k < strongElements.length; k++)
					{
						String classAttribute = strongElements[k].getAttributeByName("class");
						if(classAttribute != null && classAttribute.equals(BANDNAME))
						{
							row.setBandName(strongElements[k].getText().toString().replace("&amp;", "&").trim());
						}
					}
					
					TagNode iElements[] = trElements[j].getElementsByName("i", true);
					for(int k = 0; iElements != null && k < iElements.length; k++)
					{
						String classAttribute = iElements[k].getAttributeByName("class");
						if(classAttribute != null && classAttribute.equals(BANDDESCRIPTION))
						{
							row.setBandDescription(iElements[k].getText().toString().replace("&nbsp;", "").replace("&amp;", "&").trim());
						}
						String styleAttribute = iElements[k].getAttributeByName("style");
						if(styleAttribute != null && styleAttribute.equals(PRICEINSTYLE))
						{
							row.setPrice(iElements[k].getText().toString().replace("&nbsp;", "").replace("&amp;", "&").trim());
						}		
					}
					
					TagNode spanElements[] = trElements[j].getElementsByName("span", true);
					for(int k = 0; spanElements != null && k < spanElements.length; k++)
					{
						String classAttribute = spanElements[k].getAttributeByName("class");
						if(classAttribute != null && classAttribute.equals(PRICEINSPAN))
						{
							row.setPrice(spanElements[k].getText().toString().replace("&nbsp;", "").replace("&amp;", "&"));
						}
					}
					
					TagNode timeElements[] = trElements[j].getElementsByName("td", true);
					if(timeElements.length > 0)
					{
						row.setTime(timeElements[0].getText().toString().replace("&nbsp;", "").replace("&amp;", "&").trim());
					}
					res.add(row);
				}
			}
		}

		return res;
	}
}
